package com.echoesnet.echoesshortvideo.Repositories;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.os.Environment;

import com.echoesnet.echoesshortvideo.Beans.PublishVideoBean;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.RomUtils;
import com.echoesnet.echoesshortvideo.Utils.VideoUtilListener;
import com.echoesnet.echoesshortvideo.Utils.VideoUtils;
import com.orhanobut.logger.Logger;

import java.io.File;

/**
 * Copyright (C) 2017 在线回声（天津）科技发展有限公司
 * 在线回声完全享有此软件的著作权，违者必究
 *
 * @author an
 * @version 1.0
 * @modifier
 * @createDate 2018/4/24 0024
 * @description
 */
public class PublishRepository
{
    private final static String TAG = PublishRepository.class.getSimpleName();
    private MutableLiveData<PublishVideoBean> data;

    public LiveData<PublishVideoBean> cutVideo(String startTime, String endTime, String path){
        long time = System.currentTimeMillis();
        String outPath;
        if (RomUtils.isVivo())
        {
            outPath = Environment.getExternalStorageDirectory().getPath() + File.separator +
                    "相机" + File.separator + time + ".mp4";
        }else {
            outPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + File.separator +
                    "Camera/"+time + ".mp4";
        }
        if (data == null)
        data = new MutableLiveData();
        data.setValue(new PublishVideoBean());
        VideoUtils.cutVideo(startTime, endTime, path, outPath, new VideoUtilListener()
        {
            @Override
            public void start()
            {

            }

            @Override
            public void complete(String outPath, String err)
            {
                Logger.t(TAG).d("剪切视频完成 outPath>>" + outPath + "|err>>" + err);
                PublishVideoBean publishVideoBean = data.getValue();
                if (publishVideoBean != null)
                    publishVideoBean.setUrl(outPath);
                data.setValue(publishVideoBean);
            }
        });
        return data;
    }

    public LiveData<PublishVideoBean> getThumbnail(boolean isCamera,long startTime, String path,int degree){
        long time = System.currentTimeMillis();
        String thumbnailPath = CommonUtils.getRootDirPath(null) + "CutVideo/" + time;
        if (data == null)
        data = new MutableLiveData();
        data.setValue(new PublishVideoBean());
        if (isCamera)
        {
            VideoUtils.getMediaThumbnail(0,path, thumbnailPath, 0, true, new VideoUtilListener()
            {
                @Override
                public void start()
                {

                }

                @Override
                public void complete(String outPath, String err)
                {
                    Logger.t(TAG).d("视频首帧缩略图截取完成 outPath>>" + outPath + "|err>>" + err);
                    PublishVideoBean publishVideoBean = data.getValue();
                    if (publishVideoBean != null)
                        publishVideoBean.setThumbnails(outPath);
                    data.setValue(publishVideoBean);
                }
            });
        }else {
            VideoUtils.getVideoMediaThumbnail(startTime,degree,path, thumbnailPath, 0, true, new VideoUtilListener()
            {
                @Override
                public void start()
                {

                }

                @Override
                public void complete(String outPath, String err)
                {
                    Logger.t(TAG).d("视频首帧缩略图截取完成 outPath>>" + outPath + "|err>>" + err);
                    PublishVideoBean publishVideoBean = data.getValue();
                    if (publishVideoBean != null)
                        publishVideoBean.setThumbnails(outPath);
                    data.setValue(publishVideoBean);
                }
            });
        }

        return data;
    }



    public LiveData<PublishVideoBean> compressVideo(String path){
        long time = System.currentTimeMillis();
        String outPath = CommonUtils.getRootDirPath(null) + "CutVideo/" + time + ".mp4";
        if (data == null)
            data = new MutableLiveData();
        data.setValue(new PublishVideoBean());
//        VideoUtils.reversalVideo(path, outPath, new VideoUtilListener()
//        {
//            @Override
//            public void start()
//            {
//
//            }
//
//            @Override
//            public void complete(String outPath, String err)
//            {
//                Logger.t(TAG).d("剪切视频完成 outPath>>" + outPath + "|err>>" + err);
//                PublishVideoBean publishVideoBean = data.getValue();
//                if (publishVideoBean != null)
//                    publishVideoBean.setUrl(outPath);
//                data.setValue(publishVideoBean);
//            }
//        });
        VideoUtils.compressVideo( path, outPath, new VideoUtilListener()
        {
            @Override
            public void start()
            {

            }

            @Override
            public void complete(String outPath, String err)
            {
                Logger.t(TAG).d("剪切视频完成 outPath>>" + outPath + "|err>>" + err);
                PublishVideoBean publishVideoBean = data.getValue();
                if (publishVideoBean != null)
                    publishVideoBean.setUrl(outPath);
                data.setValue(publishVideoBean);
            }
        });
        return data;
    }
}
